﻿//生成LED命令的function



/***********************获取LED命令头部******************/
function getLEDHeadHexString(ledcommand){
    return getExtDeviceHeadHexStringl(1,0,0,ledcommand);
}

//function getLEDHeadHexString(deviceaddress,ledcommand){
//    return getDeviceHeadHexStringl(0x01,deviceaddress,0x0000,ledcommand);
//}

/***********************获取外设命令头部******************/
function getExtDeviceHeadHexStringl(devicetype,deviceaddress,messagetype,ledcommand){
    var s= new String();
    var header = "40"; //header
    var hexDeviceType =  padLeft(parseInt(devicetype).toString(16),'0',2);
    var hexDeviceAddress = padLeft(parseInt(deviceaddress).toString(16),'0',2);
    var hexMessageType = padLeft(parseInt(messagetype).toString(16),'0',4);
    var hexCommand = padLeft(parseInt(ledcommand).toString(16),'0',4);
    s = header + hexDeviceType + hexDeviceAddress + hexMessageType + hexCommand;
    return s;
}

/********************获取完整的LED命令（除校验和尾标示)*****************/
function getLEDFullHexString(ledcommand,bodyhex){
    var len = bodyhex.length/2 + 2;
    var lenhex = padLeft(parseInt(len).toString(16),'0',4);
    var rndflag = Math.floor(Math.random()*0xFFFF); //生成2字节随机flag
    var flaghex = padLeft(parseInt(rndflag).toString(16),'0',4);
    var headhex = getLEDHeadHexString(ledcommand);
    return headhex + lenhex + flaghex + bodyhex;
}

//半角转全角
function ToDBC(txtstring){
    var tmp = ""; 
    for(var i=0;i<txtstring.length;i++){ 
        if(txtstring.charCodeAt(i)==32){ 
            tmp= tmp+ String.fromCharCode(12288); 
        } 
        if(txtstring.charCodeAt(i)<127){ 
            tmp=tmp+String.fromCharCode(txtstring.charCodeAt(i)+65248); 
        }
        else
            tmp=tmp +txtstring.substr(i,1);
    }
    return tmp; 
}

//全角转换为半角函数 
function ToCDB(str){ 
    var tmp = ""; 
    for(var i=0;i<str.length;i++){ 
        if(str.charCodeAt(i)>65248&&str.charCodeAt(i)<65375){ 
            tmp += String.fromCharCode(str.charCodeAt(i)-65248); 
        }else{ 
            tmp += String.fromCharCode(str.charCodeAt(i)); 
        } 
    }
    return tmp 
}

function getAddAdvHex(address,font,color,moving,speed,stoptime,stopmodel,lifetime,showtime,timespancount,timespanarray,text){
    
}

function getImmiMsgHex(showlevel,exittype,timeseconds,showtimeseconds, showtimes,msgid,text){

}

function getAlarmMsgHex(showtype,text){

}

function sumStr(str) {//字符串实际占用字节数
    var num = str.length;
    var arr = str.match(/[^\x00-\x80]/ig);
    if (arr != null)
        num += arr.length;
    return num;
}

/***********获取川基显示速度 字节/秒****************/
function getChuanJiCharSpeed(speedlevel) {
    if (speedlevel == 0)
        return 10;
    else if (speedlevel == 1)
        return 8;
    else if (speedlevel == 2)
        return 5;
    else if (speedlevel == 3)
        return 4;
    else if (speedlevel == 4)
        return 2;
    else
        return 10;
}

/***********获取川基LED的显示时间*****
/*charlen 广告字节数
/*screenlen 屏字节数
/*direction 方向 1-left, 2 right,3 up,4 down, 5 keep
/*speed 字节/秒
/*stoptime 停留时间
******************************************/
function getChuanJiLEDShowTime(charlen, screenlen, direction, speed, stoptime) {
    var showtime = 0;
    if(direction == 1){
        //左移
        showtime = parseInt((parseFloat(charlen.toString()) + parseFloat(screenlen.toString())) / speed + (parseFloat(charlen.toString()) / parseFloat(screenlen.toString())) * stoptime);
    }
    else if (direction == 3 || direction == 4) {
        showtime = parseInt(parseFloat(charlen.toString()) / parseFloat(screenlen.toString()) / parseFloat(speed.toString()) +
         parseFloat(charlen.toString()) /parseFloat(screenlen.toString())*stoptime);
    }
    return showtime;
}